/**
 * Created by Administrator on 2017/7/4 0004.
 */

function getInputData() {
    var hostname = $('#cmdb-hosts-hostname').val().trim();
    var hostpublicip = $('#cmdb-hosts-hostpublicip').val().trim();
    var hostport = $('#cmdb-hosts-hostport').val().trim();
    var hostservices = $('#cmdb-hosts-services').val().trim();
    var hostsgroupid = $('#cmdb-hosts-hostgroup').val();
    var hostuser = $('#cmdb-hosts-hostmanager').val();
    if ($('#cmdb-hosts-location-aliyun').prop('checked')) {
        var hostlocation = 1
    } else if ($('#cmdb-hosts-location-azure').prop('checked')) {
        var hostlocation = 0
    }
    var datadict = {
        'hostname': hostname,
        'hostpublicip': hostpublicip,
        'hostport': hostport,
        'hostservices': hostservices,
        'hostgroupid': hostsgroupid,
        'hostlocation': hostlocation,
        'hostuser': hostuser,
    }
    return datadict
}


// add host
$(document).ready(function () {
    $('#cmdb-host-addhost-btn').delegate('button', 'click', function () {
        $("#cmdb-hosts-savebtn .btn-save").attr('flag','add');
        $('#cmdb-addhosts-modal').modal('show');
    });
/*    $('#cmdb-hosts-savebtn').delegate('.btn-save', 'click', function () {
        var alldata = getInputData();
        alldata['flag']='add';
        $.ajax({
            url: '/cmdb/addhost/',
            type: 'POST',
            data: alldata,
            success: function (data) {
                $('#cmdb-addhosts-modal').modal('hide');
                window.location.href = '/cmdb/hostindex/'
            }
        })
    })*/
});

//编辑时获取数据
$(document).ready(function () {
    $('#demo-foo-col-exp').find('.cmdb-hosts-edit').click(function () {
        $('#cmdb-addhosts-modal').modal('show');
        $("#cmdb-hosts-savebtn .btn-save").attr('flag','edit');
        var editself = this;
        $(editself).addClass('edithostActive');
        $(editself).parent().siblings().each(function () {
            var tds = this;
            var tag = $(tds).attr('target');
            if (tag) {
                var idtag = '#' + tag;
                if (tag == 'cmdb-hosts-hostgroup') {
                    var groupid = $(tds).attr('groupid');
                    $(idtag).val(groupid)
                } else if (tag == 'cmdb-hosts-hostuser') {
                    var userid = $(tds).attr('userid');
                    $(idtag).val(userid)
                }
                else if (tag == 'cmdb-hosts-location') {
                    var hostlocation = $(tds).text();
                    if (hostlocation == 'azure') {
                        $('#cmdb-hosts-location-azure').prop('checked', true)
                    } else if (hostlocation == 'aliyun') {
                        $('#cmdb-hosts-location-aliyun').prop('checked', true)
                    }
                } else {
                    $(idtag).val($(tds).text())
                }
            }
        });

    })
});

// 提交时获取数据
$('#cmdb-hosts-savebtn').delegate('.btn-save', 'click', function () {
    var flag = $("#cmdb-hosts-savebtn .btn-save").attr('flag');
    var alldata = getInputData();
    alldata["flag"]=flag;
    if (flag=='add'){
       console.log(alldata)
    }
    else if(flag =='edit'){
        var editself = $('#demo-foo-col-exp').find('.edithostActive');
        var hostid = $(editself).parent().parent().attr('hostid');
        alldata["hostid"] = hostid;
        console.log(hostid);
        console.log(JSON.stringify(alldata));
    }

    $.ajax({
        url: '/cmdb/addhost/',
        type: 'POST',
        data: alldata,
        success: function (data) {
            $('#cmdb-addhosts-modal').modal('hide');
            $("#cmdb-hosts-savebtn .btn-save").attr('flag','');
            window.location.href = '/cmdb/hostindex/';

        }
    })
});

//del host
$('#demo-foo-col-exp tbody').delegate('.cmdb-hosts-del', 'click', function () {
    var self = this;
    var hostid = $(self).parent().parent().attr('hostid');
    $.ajax({
        url: '/cmdb/delhost/',
        type: 'POST',
        data: {'hostid': hostid},
        success: function (data) {
            if (data == 'ok') {
                console.log(data);
                $(self).parent().parent().remove();
            }

        }
    })
});

//update  host


// add manager user
$('#cmdb-hosts-addmanager-savebtn').delegate('.btn-save', 'click', function () {
    var managername = $('#cmdb-hosts-manager-name').val().trim();
    var managerpwd = $('#cmdb-hosts-manager-pwd').val().trim();
    $.ajax({
        url: '/cmdb/addmanageruser/',
        type: 'POST',
        data: {'managername': managername, 'managerpwd': managerpwd},
        success: function (data) {
            var ret = JSON.parse(data);
            var managerid = ret.userid;
            var td1 = '<td>' + managername + '</td>';
            var td2 = '<td class="cmdb-hosts-deluser-btn">删除</td>';
            var trtag = document.createElement('tr');
            $(trtag).append(td1);
            $(trtag).append(td2);
            $(trtag).attr('managerid', managerid);
            $('#cmdb-hosts-delmanageruser').append(trtag);

        }
    });
    $('#cmdb-host-addhostManger-modal').modal('hide');
    $('.modal-backdrop').remove()
});
//del manager user
$('#cmdb-hosts-delmanageruser').delegate('.cmdb-hosts-deluser-btn', 'click', function () {
    var delself = this;
    var userid = $(delself).parent().attr('managerid');
    $.ajax({
        url: '/cmdb/delmanageruser/',
        type: 'POST',
        data: {'userid': userid},
        success: function (data) {
            if (data == 'ok') {
                $(delself).parent().remove();
            }
        }
    })
});

//clear dirty data
$('#cmdb-addhosts-modal').on('hidden.bs.modal', function () {
    $('#cmdb-hosts-hostname').val('');
    $('#cmdb-hosts-hostpublicip').val('');
    $('#cmdb-hosts-hostport').val('');
    $('#cmdb-hosts-services').val('');
});

$('#cmdb-host-addhostManger-modal').on('hidden.bs.modal', function () {
    $('#cmdb-hosts-manager-name').val('');
    $('#cmdb-hosts-manager-pwd').val('');
});